<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en-US">
<head>
    <meta charset="utf-8">


    <link href="/assets/css/all.css" rel="stylesheet" type="text/css">
    <script async src="/assets/js/all.js"></script>


    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({tex2jax: {inlineMath: [['«', '»']]}});
      MathJax.Hub.config.tex2jax.skipTags = ["script", "noscript", "style", "textarea", "annotation", "annotation-xml"];

    </script>
    <script async src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.2/MathJax.js?config=TeX-AMS_HTML'></script>
    <!-- Google Analytics -->
    <script>
        window.ga = window.ga || function () {
            (ga.q = ga.q || []).push(arguments)
        };
        ga.l = +new Date;
        ga('create', 'UA-58002512-1', 'auto');
        ga('set', 'anonymizeIp', true);
        ga('send', 'pageview');
    </script>
    <script async src='https://www.google-analytics.com/analytics.js'></script>
    <!-- End Google Analytics -->

    <title>Nim by Example - Strings</title>

    <meta content="nanoc 4.9.9" name="generator">
    <meta content="colorful" http-equiv="Default-Style">
    <meta content="width=device-width, initial-scale=1" name=viewport>
</head>
<body>

<div id="sidebar">
    <div class="abs-hamburger">
        <div class="nav-toggle" onclick="sidebarClick();"><span></span></div>
    </div>
    <nav>
        <ul>
            <li><a href="/getting_started/">Getting Started</a></li>
            <li><a href="/hello_world/">Hello World</a></li>
            <li><a href="/comments/">Comments</a></li>
            <li><a href="/variables/">Variables</a>
                <ul>
                    <li><a href="/variables/result/">Result</a></li>
                    <li><a href="/variables/type_casting_inference/">Type Casting and Inference</a></li>
                </ul>
            </li>
            <li><a href="/if_else_while/">If, Else, While</a></li>
            <li><a href="/case/">Case Statements</a></li>
            <li><a href="/for_iterators/">For Loops &amp; Iterators</a></li>
            <li><a href="/procs/">Procs</a></li>
            <li><a href="/procvars/">First Class Functions</a></li>
            <li><a href="/block/">Blocks</a></li>
            <li><a href="/primitives/">Primitive Types</a></li>
            <li><a href="/types/">Type Aliases</a></li>
            <li><a href="/types/objects/">Object Types</a></li>
            <li><a href="/types/enums/">Enum Types</a></li>
            <li><a href="/types/distinct/">Distinct Types</a></li>
            <li><a href="/strings/">Strings</a></li>
            <li><a href="/arrays/">Arrays</a></li>
            <li><a href="/seqs/">Seqs</a></li>
            <li><a href="/bitsets/">Bitsets</a></li>
            <li><a href="/files/">Files</a></li>
            <li><a href="/json/">JSON</a></li>
            <li><a href="/varargs/">Varargs</a></li>
            <li><a href="/oop/">Object Oriented Programming</a></li>
            <li><a href="/macros/">Macros</a></li>
        </ul>


    </nav>
</div>

<article>
    <h1 id="strings">Strings</h1>

    <pre><code class="language-nimrod"><span class="n">echo</span> <span class="s">"words words words ⚑"</span>
<span class="n">echo</span> <span class="s">"""</span>
<span class="s">&lt;html&gt;</span>
<span class="s">  &lt;head&gt;</span>
<span class="s">  &lt;/head&gt;\n\n</span>

<span class="s">  &lt;body&gt;</span>
<span class="s">  &lt;/body&gt;</span>
<span class="s">&lt;/html&gt; """</span>

<span class="k">proc </span><span class="nf">re</span><span class="p">(</span><span class="n">s</span><span
                class="p">:</span> <span class="kt">string</span><span class="p">):</span> <span
                class="kt">string</span> <span class="o">=</span> <span class="n">s</span>

<span class="n">echo</span> <span class="s">r".</span><span class="se">""</span><span class="s">."</span>
<span class="n">echo</span> <span class="s">re"\b[a-z]++\b"</span></code></pre>
    <pre><code class="language-console"><span class="gp">$</span> nim c -r strings.nim
<span class="go">words words words ⚑</span>
<span class="go">&lt;html&gt;</span>
<span class="go">  &lt;head&gt;</span>
<span class="go">  &lt;head/&gt;\n\n</span>

<span class="go">  &lt;body&gt;</span>
<span class="go">  &lt;body/&gt;</span>
<span class="go">&lt;html/&gt;</span>
<span class="go">.".</span>
<span class="go">\b[a-z]++\b</span></code></pre>

    <p>There are several types of string literals:</p>

    <ul>
        <li>Quoted Strings: Created by wrapping the body in triple quotes, they never interpret escape codes</li>
        <li>Raw Strings: created by prefixing the string with an <code>r</code>. They do not interpret escape sequences,
            except for <code>""</code>, which is interpreted as <code>"</code>. This means that
            <code>r"\b[a-z]\b"</code> is interpreted as <code>\b[a-z]\b</code> instead of failing to compile with a
            syntax error.
        </li>
        <li>Proc Strings: raw strings, but the method name that prefixes the string is called, so that
            <code>foo"12\"</code> -&gt; <code>foo(r"12\")</code>.
        </li>
    </ul>

    <p>Strings are null-terminated, so that <code>cstring("foo")</code> requires zero copying. However, you should be
        careful that the lifetime of the cstring does not exceed the lifetime of the string it is based upon.</p>

    <p>Strings can also almost be thought of as <code>seq[char]</code> with respect to assignment semantics. See <a
            href="../seqs#immutability">seqs</a> for the possible manipulations, for example:</p>

    <pre><code class="language-nimrod"><span class="n">echo</span> <span class="s">"abc"</span><span
            class="o">[</span><span class="mi">0</span><span class="o">]</span>
<span class="n">echo</span> <span class="s">"abcdefg"</span><span class="o">[</span><span class="mi">0</span> <span
                class="p">..</span> <span class="mi">4</span><span class="o">]</span>
<span class="n">echo</span> <span class="s">"abcdefg"</span><span class="o">[</span><span class="mi">0</span> <span
                class="p">..</span> <span class="p">^</span><span class="mi">2</span><span
                class="o">]</span></code></pre>
    <pre><code class="language-console"><span class="gp">$</span> nim c -r strindex.nim
<span class="go">a</span>
<span class="go">abcde</span>
<span class="go">abcdef</span></code></pre>

    <p>The <code>strutils</code> module provides procs for handling strings.</p>

    <pre><code class="language-nimrod"><span class="kn">import</span> <span class="n">strutils</span>

<span class="kd">var</span> <span class="n">a</span> <span class="o">=</span> <span
                class="s">"hello welcome,friend"</span>

<span class="c"># The split proc takes a sequence of characters and splits a string based on them</span>
<span class="n">echo</span> <span class="n">a</span><span class="p">.</span><span class="n">split</span><span class="p">({</span><span
                class="sc">' '</span><span class="p">,</span> <span class="sc">','</span><span class="p">})</span>

<span class="c"># The contains proc determines whether a string contains a substring or character</span>
<span class="n">echo</span> <span class="n">a</span><span class="p">.</span><span class="n">contains</span><span
                class="p">(</span><span class="s">"hello"</span><span class="p">)</span></code></pre>

    <pre><code class="language-console"><span class="gp">$</span> nim c -r strutils.nim
<span class="go">@["hello", "welcome", "friend"]</span>
<span class="go">true</span></code></pre>

    <h2 id="a-note-about-unicode">A note about Unicode</h2>
    <p>Unicode symbols are allowed in strings, but are not treated in any special way, so if you want count glyphs or
        uppercase Unicode symbols, you must use the <code>unicode</code> module.</p>

    <p>Strings are generally considered to be encoded as UTF-8, so because of Unicode’s backwards compatibility, can be
        treated exactly as ASCII, with all values above 127 ignored.</p>

</article>

<div id=nextprev>
    <a class="text-icon disabled" href="https://please-enable-js/" id=arrow-prev>↽</a>
    <a class="text-icon disabled" href="https://please-enable-js/" id=arrow-next>⇁</a>
</div>

<footer>
    <li><a href="https://github.com/flaviut/nim-by-example">Contribute</a></li>
    <li
    ><a href="#" onclick="toggleDarkMode(); return false;">Toggle dark mode</a></li>

</footer>
</body>
</html>
